package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.model.rdb.AudienceManager;
import com.ndp.fb.rdb.api.AudienceManagerRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.util.SQLUtil;

import java.util.List;

/**
 * Created by sisi.chen on 2016/4/20.
 */
@Service
public class AudienceManagerRdbServiceImpl extends DaoTemplate implements AudienceManagerRdbService {
    @Override
    public Long save(AudienceManager audienceManager) {
        return saveResultId(audienceManager);
    }

    @Override
    public Pager<AudienceManager> findByUserId(String userId, Integer firstResult, Integer sizeNo) {
        String sql = "select * from audience_manager where user_id = ? and is_delete = 0 order by create_time desc";
        return findPager(sql, new Object[]{userId}, firstResult, sizeNo, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public AudienceManager findById(Long id) {
        String sql = "select * from audience_manager where id=? and is_delete = 0  ";
        return findOne(sql, new Object[]{id}, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public List<AudienceManager> findByUserIdAndWeFeedIdAndName(String userId, Long weFeedId, String name) {
        String sql = "select * from audience_manager where user_id = ? and name = ? and we_feed_id = ? and is_delete = 0";
        return getJdbcTemplate().query(sql, new Object[]{userId, name, weFeedId}, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public List<AudienceManager> findByUserIdAndWeFeedIdAndAndFbAccountIdName(String userId, Long weFeedId, Long fbAccountId, String name) {
        String sql = "select * from audience_manager where user_id = ? and name = ? and we_feed_id = ? and fb_account_id = ? and is_delete = 0";
        return getJdbcTemplate().query(sql, new Object[]{userId, name, weFeedId, fbAccountId}, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public Pager<AudienceManager> findByFeed(Long weFeedId, Integer firstResult, Integer sizeNo) {
        String sql = "SELECT audience_manager.*, entrust_account.name AS account_name FROM audience_manager \n" +
                "LEFT JOIN `entrust_account` ON audience_manager.`fb_account_id` = entrust_account.`fb_account_id`\n" +
                "WHERE  audience_manager.we_feed_id = ? AND audience_manager.is_delete = 0 ORDER BY audience_manager.create_time DESC";
        return findPager(sql, new Object[]{weFeedId}, firstResult, sizeNo, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public List<AudienceManager> findAll() {
        String sql = "select * from audience_manager where is_delete = 0";
        return getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public List<AudienceManager> findAllByFeed(Long weFeedId) {
        String sql = "select * from audience_manager where we_feed_id = ? and is_delete = 0 order by create_time desc";
        return getJdbcTemplate().query(sql, new Object[]{weFeedId}, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public List<AudienceManager> findAllByFeedAndFbAccountId(Long weFeedId, Long fbAccountId) {
        String sql = "select * from audience_manager where we_feed_id = ? and fb_account_id = ? and is_delete = 0 order by create_time desc";
        return getJdbcTemplate().query(sql, new Object[]{weFeedId, fbAccountId}, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }


    @Override
    public void update(AudienceManager audienceManager) {
        String sql = "update audience_manager set user_id = ?," +
                "fb_account_id = ?,we_feed_id = ?,product_catalog_id = ?," +
                "product_set_id = ?,name = ?," +
                "description = ?," +
                "local_targeting = ?," +
                "fb_targeting = ?," +
                "potential_user = ?," +
                "is_delete = ?,update_time = ?,create_time = ? where id = ? ";

        getJdbcTemplate().update(sql, new Object[]{audienceManager.getUserId(), audienceManager.getFbAccountId(),
                audienceManager.getWeFeedId(), audienceManager.getProductCatalogId(), audienceManager.getProductSetId(),
                audienceManager.getName(), audienceManager.getDescription(), audienceManager.getLocalTargeting(),
                audienceManager.getFbTargeting(), audienceManager.getPotentialUser(), audienceManager.getIsDelete(), audienceManager.getUpdateTime(),
                audienceManager.getCreateTime(),
                audienceManager.getId()});
    }

    @Override
    public List<AudienceManager> findByWeFeedIdAndNames(String feedId, List<String> audienceTemplate) {
        String sql = "select * from audience_manager where we_feed_id = ? and is_delete = 0 and name in (" + SQLUtil.appendPlaceholder(audienceTemplate.size()) + ")";
        audienceTemplate.add(0, feedId);
        return getJdbcTemplate().query(sql, audienceTemplate.toArray(), DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }

    @Override
    public List<AudienceManager> findByIds(Object[] objects) {
        String sql = "select * from audience_manager where id in (" + SQLUtil.appendPlaceholder(objects.length) + ")";
        return getJdbcTemplate().query(sql, objects, DefaultRowMapper.autoRowMapper(AudienceManager.class));
    }
}
